package com.zzl.bonds.ticket.controller;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.bonds.ticket.constant.status.TicketStatusEnum;
import com.zzl.bonds.ticket.constant.type.KeyTypeEnum;
import com.zzl.bonds.ticket.dto.StatisticsDataDto;
import com.zzl.bonds.ticket.dto.TicketTransferInfo;
import com.zzl.bonds.ticket.dto.TicketTransferPage;
import com.zzl.bonds.ticket.dto.TicketransferAddDto;
import com.zzl.bonds.ticket.dto.feign.ApprovalSubmitDto;
import com.zzl.bonds.ticket.service.TicketTransferService;
import com.zzl.general.common.apidoc.annotation.ApidocParamsPage;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

/**
 * 转让申请单
 *
 * @author sunpp
 * @date 2022-02-16 11:14:07
 */
@ApidocController("转让申请单")
@RestController
@SuppressWarnings("javadoc")
@RequestMapping("bondsticket/ticketransfer")
@Slf4j
public class TicketransferController extends WebBaseCtrl {
    @SuppressWarnings("all")
    @Autowired
    private TicketTransferService ticketTransferService;

    /**
     * 分页查询转让申请凭证
     */
    @GetMapping("/list")
    @ApidocOperation(description = "分页查询转让申请凭证", operationId = "/ticket/ticketransfer/list")
    @ApidocReturnPage(genericTypeClass = TicketTransferPage.class)
    public Result list(@ApidocParamsPage(name = "params", keyName = {
            "ticketNo"
            , "receiveEnterpriseName"
            , "createStartTime"
            , "createEndTime"
            , "startAmount"
            , "endAmount"
            , "handStatus"
            , "page"
            , "order"
    }
            , keyDescription = {
            "凭证编号"
            , "收单企业"
            , "转让开始日期"
            , "转让结束日期"
            , "开始金额"
            , "结束金额"
            , "处理状态，见接口与原型 依次 1234"
            , "排序类型：create_time（默认）、invoice_time：开单日期、cash_time：到期日期，ticket_amount：凭证金额"
            , "排序方式：desc 降序（默认）asc 升序"})
                       @RequestParam Map<String, Object> params) {
        params.put("enterpriseId", this.getUser().getEnterpriseId());
        PageUtils page = ticketTransferService.queryPage(params, this.getUser(), KeyTypeEnum.KEY_TYPE_DETAILS.getCode());
        return Result.ok().put("page", page);
    }

    /**
     * 分页查询转让待审核凭证
     */
    @GetMapping("/listApprove")
    @ApidocOperation(description = "分页查询转让待审核凭证", operationId = "/ticket/ticketransfer/listApprove")
    @ApidocReturnPage(genericTypeClass = TicketTransferPage.class)
    public Result listApprove(@ApidocParamsPage(name = "params", keyName = {
            "ticketNo"
            , "receiveEnterpriseName"
            , "createStartTime"
            , "createEndTime"
            , "startAmount"
            , "endAmount"
            , "handStatus"
            , "page"
            , "order"
    }
            , keyDescription = {
            "凭证编号"
            , "收单企业"
            , "转让开始日期"
            , "转让结束日期"
            , "开始金额"
            , "结束金额"
            , "处理状态，见接口与原型 依次 123"
            , "排序类型：create_time（默认）、invoice_time：开单日期、cash_time：到期日期，ticket_amount：凭证金额"
            , "排序方式：desc 降序（默认）asc 升序"})
                              @RequestParam Map<String, Object> params) {
        params.put("ticketStatusList", Collections.singletonList(TicketStatusEnum.TRANSFER_PENDING_REVIEW.getCode()));
        params.put("enterpriseId", this.getUser().getEnterpriseId());
        PageUtils page = ticketTransferService.queryPageApprove(params, this.getUser());
        return Result.ok().put("page", page);
    }

    /**
     * 分页查询转让签收凭证列表
     */
    @GetMapping("/listSignApprove")
    @ApidocOperation(description = "分页查询转让签收凭证列表", operationId = "/ticket/ticketransfer/listSignApprove")
    @ApidocReturnPage(genericTypeClass = TicketTransferPage.class)
    public Result listSignApprove(@ApidocParamsPage(name = "params", keyName = {
            "ticketNo"
            , "receiveEnterpriseName"
            , "createStartTime"
            , "createEndTime"
            , "startAmount"
            , "endAmount"
            , "handStatus"
            , "page"
            , "order"
    }
            , keyDescription = {
            "凭证编号"
            , "收单企业"
            , "转让开始日期"
            , "转让结束日期"
            , "开始金额"
            , "结束金额"
            , "处理状态，见接口与原型 依次 123"
            , "排序类型：create_time（默认）、invoice_time：开单日期、cash_time：到期日期，ticket_amount：凭证金额"
            , "排序方式：desc 降序（默认）asc 升序"})
                                  @RequestParam Map<String, Object> params) {
        params.put("ticketStatus", TicketStatusEnum.TRANSFER_PENDING_SIGN.getCode());
        params.put("receiveEnterpriseId", this.getUser().getEnterpriseId());
        PageUtils page = ticketTransferService.listSignApprove(params, this.getUser());
        return Result.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApidocOperation(description = "通过id获取转让申请单数据", operationId = "9002")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", valueClass = TicketTransferInfo.class)
    })
    public Result info(@ApidocParam(name = "转让申请单id", required = true) @PathVariable("id") String id) {
        TicketTransferInfo ticketransfer = ticketTransferService.info(id);
        return Result.ok().put("data", ticketransfer);
    }

    /**
     * 转让复核详情
     */
    @GetMapping("/recheckInfo/{id}")
    @ApidocOperation(description = "转让复核详情", operationId = "9002")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", valueClass = TicketTransferInfo.class)
    })
    public Result recheckInfo(@ApidocParam(name = "转让申请单id", required = true) @PathVariable("id") String id) {
        TicketTransferInfo ticketransfer = null;
        try {
            ticketransfer = ticketTransferService.recheckInfo(id);
            return Result.ok().put("data", ticketransfer);
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }
    /**
     * 信息
     */
    @GetMapping("/signInfo/{id}")
    @ApidocOperation(description = "转让签收详情", operationId = "9002")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", valueClass = TicketTransferInfo.class)
    })
    public Result signInfo(@ApidocParam(name = "转让申请单id", required = true) @PathVariable("id") String id) {
        TicketTransferInfo ticketransfer = null;
        try {
            ticketransfer = ticketTransferService.signInfo(id);
            return Result.ok().put("data", ticketransfer);
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApidocOperation(description = "保存转让申请单", operationId = "90031111")
    @ApidocReturnR
    public Result save(@ApidocParam(name = "保存的转让申请单对象值", required = true) @Validated @RequestBody TicketransferAddDto ticketransferAddDto) {
        log.info(ticketransferAddDto.toString());
        try {
            return Result.ok().data(ticketTransferService.saveTicketransfer(ticketransferAddDto, this.getUser()));
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }


    /**
     * 复核转让申请单
     */
    @PutMapping("/recheckTransferOrder")
    @ApidocOperation(description = "复核转让申请单", operationId = "9004")
    @ApidocReturnR
    public Result recheckTransferOrder(@ApidocParam(name = "修改的转让申请单对象值", required = true) @Validated @RequestBody ApprovalSubmitDto approvalSubmitDto) {
        try {
            ticketTransferService.recheckTransferOrder(approvalSubmitDto, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 转让签收
     */
    @PutMapping("/signTransferOrder")
    @ApidocOperation(description = "转让签收申请单", operationId = "9024/1")
    @ApidocReturnR
    public Result signTransferOrder(@ApidocParam(name = "修改的转让申请单对象值", required = true) @Validated @RequestBody ApprovalSubmitDto ticketransfer) {
        try {
            ticketTransferService.signTransferOrder(ticketransfer, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApidocOperation(description = "删除转让申请单", operationId = "9005")
    @ApidocReturnR
    public Result delete(@ApidocParam(name = "删除的转让申请单对象id集合", required = true) @RequestBody String[] ids) {
        ticketTransferService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

    @GetMapping("/getTransferData")
    @ApidocOperation(description = "(1)转让页面的三个统计数量", operationId = "ticket/ticketransfer/getTransferData")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", genericTypeClass = StatisticsDataDto.class)
    })
    public Result getTransferData() {
        return Result.ok().put("data", ticketTransferService.getTransferData(this.getUser()));
    }

    @GetMapping("/getTransferSignData")
    @ApidocOperation(description = "(1)转让签收页面的三个统计数量", operationId = "ticket/ticketopen/getOpenSignData")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", genericTypeClass = StatisticsDataDto.class)
    })
    public Result getTransferSignData() {
        return Result.ok().put("data", ticketTransferService.getTransferSignData(this.getUser()));
    }

    @GetMapping("/cancelTransferTicket/{id}")
    @ApidocOperation(description = "撤销转单", operationId = "/ticket/ticketransfer/cancelTransferTicket")
    @ApidocReturnR
    public Result cancelTransferTicket(@ApidocParam(name = "撤销开单", required = true) @PathVariable("id") String id) {
        try {
            ticketTransferService.cancelTransferTicket(id, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }
}
